/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.NhanVienDAO;
import Entity.NhanVien;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Dark_Knight
 */
public class NhanVienImpl implements NhanVienBUS {

    private NhanVienDAO dao;
    
    public NhanVienImpl() {
        dao = new DAO.NhanVienImpl();
    }
    
    public boolean add(NhanVien data, int MaQuyen) {
        if (data.getTenDangNhap().isEmpty() ||
            data.getHoTenNV().isEmpty() ||
            data.getMatKhau().isEmpty() )
            return false;
        
        return dao.add(data, MaQuyen);
    }

    public boolean edit(NhanVien data, int MaQuyen) {
        return dao.edit(data, MaQuyen);
    }

    public boolean delete(int id) {
        return dao.delete(id);
    }

    public void getAllDetail(DefaultTableModel model) {
        ResultSet rs = dao.getAll();        
        try {
            while (rs.next()) {
                Object[] data = {
                    rs.getInt(1),       //MaNhanVien
                    rs.getString(2),    //HoTen
                    rs.getBoolean(3) == true ? "Nam" : "Nữ",   //GioiTinh
                    rs.getString(4),    //DiaChi
                    rs.getString(5),    //SDT
                    rs.getString(6),    //Email
                    rs.getInt(7),       //MaQuyen
                    rs.getString(8)     //TenQuyen
                };
                model.addRow(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(NhanVienImpl.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }    

    public NhanVien checkLogin(String usename, String pass) {
        ResultSet rs = dao.checkLogin(usename, pass);
        NhanVien data = null;

        if (rs == null) {
            return null;
        }

        try {
            while (rs.next()) {
                data = new NhanVien(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getBoolean(3),
                        rs.getString(7),
                        null,
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6));
                return data;
            }
        } catch (SQLException ex) {
            Logger.getLogger(NhanVienImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

        return null;
    }

    public boolean changePass(int MaNV, String oldPass, String newPass) {
        return dao.changePass(MaNV, oldPass, newPass);
    }
}
